//
//  VVYShareMessage.m
//  viviyoo
//
//  Created by viviyoo on 15/11/27.
//  Copyright © 2015年 viviyoo. All rights reserved.
//

#import "VVYShareMessage.h"
#import "VVSharedView.h"
#import "UMSocial.h"


@interface VVYShareMessage ()<UMSocialUIDelegate>


@end

@implementation VVYShareMessage



//static id _instance;
//
//+ (instancetype)allocWithZone:(struct _NSZone *)zone
//{
//    @synchronized(self) {
//        if (_instance == nil) {
//            _instance = [super allocWithZone:zone];
//        }
//    }
//    return _instance;
//}
//
//+ (instancetype)sharedInstance
//{
//    @synchronized(self) {
//        if (_instance == nil) {
//            _instance = [[self alloc] init];
//        }
//    }
//    return _instance;
//}
//
//- (id)copyWithZone:(NSZone *)zone
//{
//    return _instance;
//}

#pragma mark - 默认样式
+ (void)shareMessageWithPresentSnsIconSheetView:(UIViewController *)vc
                                      shareText:(NSString *)shareText
                                     shareImage:(UIImage *)shareImage
                                       shareUrl:(NSString *)url
{
    
    //    [UMSocialSnsService presentSnsIconSheetView:vc
    //                                         appKey:nil
    //                                      shareText:shareText
    //                                     shareImage:shareImage
    //                                shareToSnsNames:@[UMShareToWechatSession,UMShareToWechatTimeline,UMShareToWechatFavorite,UMShareToQQ,UMShareToQzone,UMShareToSina,UMShareToSms]
    //                                       delegate:self];
    
//    ShareType shareType = ShareTypeDefault;
//    
//    if (url == nil) {
//        url = SharedLink;
//    }
    //如果内容为空就是纯图片类型
//    if (shareText == nil) {
//        
//         shareType = ShareTypeImageOnly;
//        
//        [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeText;
//        [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeImage;
//    }else{//有内容就是图文类型
//    
//        [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeNone;
//        [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
//        
        [UMSocialData defaultData].extConfig.wechatSessionData.title = ShareTitle;
        [UMSocialData defaultData].extConfig.wechatTimelineData.title = nil;
        [UMSocialData defaultData].extConfig.qqData.title = ShareTitle;
        [UMSocialData defaultData].extConfig.qzoneData.title = ShareTitle;
//
//        
//        
//        [UMSocialData defaultData].extConfig.wechatSessionData.url = url;
//        [UMSocialData defaultData].extConfig.wechatTimelineData.url = url;
//        [UMSocialData defaultData].extConfig.qqData.url = url;
//        [UMSocialData defaultData].extConfig.qzoneData.url = url;
//    }
    

    
    
    [VVSharedView showWithBlock:^(NSString *type) {
        
        //判断是不是判断链接
        if (type == CopyLinkShare) {
            
            [MBProgressHUD showOnlyText:@"复制文本成功"];
            
            [[UIPasteboard generalPasteboard] setPersistent:YES];
            [[UIPasteboard generalPasteboard] setValue:shareText forPasteboardType:[UIPasteboardTypeListString objectAtIndex:0]];
            
        }else{
            [self shareWithTypes:type content:shareText image:shareImage location:nil url:url presentedController:vc];
        }
        }];
    
}





/**
 关闭当前页面之后
 
 @param fromViewControllerType 关闭的页面类型
 
 */
-(void)didCloseUIViewController:(UMSViewControllerType)fromViewControllerType{
    
    
    //NSlog(@"分享结束");
}



#pragma mark - 自定义样式

+ (void)shareWithTypes:(NSString *)platformTypes
               content:(NSString *)content
                 image:(id)image
              location:(CLLocation *)location
                   url:(NSString *)url
   presentedController:(UIViewController *)presentedController
{

    
    NSDictionary *typeDict = @{
                               WechatShare : UMShareToWechatSession,
                               WechatTimeLineShare  : UMShareToWechatTimeline,
                               QQShare : UMShareToQQ,
                               QQZoneShare : UMShareToQzone,
                               WeiboShare : UMShareToSina,
                               SMSShare : UMShareToSms
                               };
    
    platformTypes = typeDict[platformTypes];
    
    //定义回调
    UMSocialDataServiceCompletion completion = nil;
    
    //如果是微博和短信就把图片去掉，换上链接
    if (platformTypes == UMShareToSina) {
        
        content = [content stringByAppendingString:url];
        
        UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
        
        [[UMSocialControllerService defaultControllerService] setShareText:content shareImage:image socialUIDelegate:vc];        //设置分享内容和回调对象
        [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina].snsClickHandler(vc,[UMSocialControllerService defaultControllerService],YES);
        return;
        
    }else if(platformTypes == UMShareToSms){
        
        content = [content stringByAppendingString:url];
        image = nil;
    
    }
    
    
    [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[platformTypes]
                                                        content:content
                                                          image:image
                                                       location:location
                                                    urlResource:nil
                                            presentedController:presentedController
                                                     completion:completion];
    
}
@end
